package huang.com.android.spinner;

import android.app.Activity;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class SpinnerActivity extends Activity {
    private static final String[] cities = { "广州", "从化", "武汉", "汕头" };
    private TextView myTextView;
    private EditText myEditText;
    private Button myButton_add;
    private Button MyButton_remove;
    private Spinner mySpinner;
    private ArrayAdapter adapter;
    private List allCities;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_spinner);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        allCities = new ArrayList();
        for (int i = 0; i < cities.length;i++){
            allCities.add(cities[i]);
        }

        /*new ArrayAdapter物件并将allCities传入*/
        adapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item,allCities);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
        /*以findViewById()取得对象*/
        myTextView = (TextView) findViewById(R.id.myTextView);
        myEditText = (EditText) findViewById(R.id.myEditText);
        myButton_add = (Button) findViewById(R.id.myButton_add);
        MyButton_remove = (Button) findViewById(R.id.myButton_remove);
        mySpinner = (Spinner) findViewById(R.id.mySpinner);

        /*将ArrayAdapter加入Spinner对象中*/
        mySpinner.setAdapter(adapter);
        /*将myButton_add加入OnClickListenner*/
        myButton_add.setOnClickListener(new Button.OnClickListener(){

            @Override
            public void onClick(View v) {
                String newCities = myEditText.getText().toString();
                /*先对比新增的值是否已存在，不存在才可新增*/
                for (int i = 0;i < adapter.getCount();i++){
                    if (newCities.equals(adapter.getItem(i))){
                        return;
                    }
                }
                if (!newCities.equals(""))/*如果不为空*/
                {
                    /*将值新增至adapter*/
                    adapter.add(newCities);
                    /*取得新增的值的位置*/
                    int position = adapter.getPosition(newCities);
                    /*将spinner选取在新增的值得位置*/
                    mySpinner.setSelection(position);
                    /*将myEditText清空，感觉从输入框的结果飞入spinner中*/
                    myEditText.setText("");
                }
            }
        });
        /*将myButton_remove加入OnClickListener*/
        MyButton_remove.setOnClickListener(new Button.OnClickListener(){
            @Override
            public void onClick(View v) {
                if (mySpinner.getSelectedItem() != null){
                    /*移除mySpinner的值*/
                    adapter.remove(mySpinner.getSelectedItem().toString());
                    /*将myEditText清空*/
                    myEditText.setText("");
                    if (adapter.getCount() == 0){
                        /*将myTextView清空*/
                        myTextView.setText("");
                    }
                }
            }
        });

        /*mySpinner加入OnitemSelectedListener*/
        mySpinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                /*将所选mySpinner的值带入myTextView中*/
                myTextView.setText(parent.getSelectedItem().toString());

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    private void setSupportActionBar(Toolbar toolbar) {
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_spinner, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
